import axios from "axios";
import config from "./config";
import { stringify } from "qs";
import AsyncStorage from "@react-native-community/async-storage";

export default function request(url, params, success, fail) {
  url = config.website + "/v1/" + url;
  if (!params) {
    params = {};
  }
  AsyncStorage.getItem("Token", (err, token) => {
    if (!err) {
      params.Token = token;
    }
    console.log("request", `${url}?${stringify(params)}`);
    axios
      .get(url, { params })
      .then(res => {
        res = res.data;
        if (res.code == 0 && success) {
          success(res.data, res.total);
        } else if (res.code != 0 && fail) {
          fail(res.data, res.code);
        } else if (res.code == -1) {
          console.log("退出登录");
        }
        return res;
      })
      .catch(err => {
        console.log("catch", err);
      });
  });
}
